package com.jrtc.base.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    //获取当前时间+天数之后的时间
    public static Date getCurrDateAddDay(Integer dayNumber){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, dayNumber);

        return c.getTime();
    }

    //获取某个时间+天数之后的时间
    public static Date getCurrDateAddDay(Date date,Integer dayNumber){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, dayNumber);

        return c.getTime();
    }

    //根据当前日期获取所在周的日期
    @SuppressWarnings("deprecation")
    public static List<Date> dateToweek(Date mdate){
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++){
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, fdate);
        }
        return list;
    }

    //根据当前时间获取本月所有日期
    public static List<Date> getAllTheDateOftheMonth(Date date){
        List<Date> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month){
            list.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }

    /**
     * 获取未来一周的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> getDayList() {
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <7; i++) {
            fetureDaysList.add(getFetureDate(i));
        }
        return fetureDaysList;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
}
